/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuEditPanel.java
 *
 * Created on Mar 29, 2010, 9:15:49 PM
 */

package edu.asu.cse360.client.ui.frame;

import edu.asu.cse360.client.context.RestaurantContext;
import edu.asu.cse360.client.util.Formatter;
import edu.asu.cse360.context.ServiceFactory;
import edu.asu.cse360.dto.ImageDto;
import edu.asu.cse360.model.MenuItem;
import edu.asu.cse360.model.Photo;
import edu.asu.cse360.service.MenuItemService;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *This class allows the manager to edit the menu
 * @author ggreiner
 */
public class MenuEditPanel extends javax.swing.JPanel {

    private static MenuEditPanel instance;

    public static MenuEditPanel instance() {
        if(instance == null){
            instance = new MenuEditPanel();
        }
        return instance;
    }

    private MenuItem selectedMenuItem;
    private MenuItemTableModel tableModel;
    private Photo photo;

    private MenuItemService menuItemSvc;

    

    /** Creates new form MenuEditPanel */
    private MenuEditPanel() {
        menuItemSvc = ServiceFactory.getMenuItemService();
        tableModel = new MenuItemTableModel();
        initComponents();
        refreshModel();
        if(tableModel.getMenuItems().size() > 0){
            selectedMenuItem = tableModel.getItemAtIndex(0);
            updateMenuItemFields();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        infoTabbedPane = new javax.swing.JTabbedPane();
        generalPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        ratingTxt = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        descriptionTxt = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        nameTxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        priceTxt = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCookTime = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        photoLbl = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        jTable1.setModel(getTableModel());
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel3.setText("Rating:");

        ratingTxt.setEnabled(false);
        ratingTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ratingTxtActionPerformed(evt);
            }
        });

        descriptionTxt.setColumns(20);
        descriptionTxt.setRows(5);
        jScrollPane2.setViewportView(descriptionTxt);

        jLabel2.setText("Description:");

        jLabel1.setText("Name:");

        jLabel4.setText("Price:");

        priceTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priceTxtActionPerformed(evt);
            }
        });

        jLabel5.setText("Time to Cook:");

        org.jdesktop.layout.GroupLayout generalPanelLayout = new org.jdesktop.layout.GroupLayout(generalPanel);
        generalPanel.setLayout(generalPanelLayout);
        generalPanelLayout.setHorizontalGroup(
            generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(generalPanelLayout.createSequentialGroup()
                .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, generalPanelLayout.createSequentialGroup()
                        .add(55, 55, 55)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nameTxt, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE))
                    .add(generalPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE))
                    .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, generalPanelLayout.createSequentialGroup()
                            .add(20, 20, 20)
                            .add(jLabel5)
                            .add(18, 18, 18)
                            .add(txtCookTime))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, generalPanelLayout.createSequentialGroup()
                            .add(50, 50, 50)
                            .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(generalPanelLayout.createSequentialGroup()
                                    .add(jLabel3)
                                    .add(11, 11, 11)
                                    .add(ratingTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(generalPanelLayout.createSequentialGroup()
                                    .add(10, 10, 10)
                                    .add(jLabel4)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(priceTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
        generalPanelLayout.setVerticalGroup(
            generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(generalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(nameTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(27, 27, 27)
                .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(txtCookTime, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(11, 11, 11)
                .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(priceTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(generalPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(generalPanelLayout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(jLabel3))
                    .add(ratingTxt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(164, 164, 164))
        );

        infoTabbedPane.addTab("General", generalPanel);

        photoLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        photoLbl.setText("Loading Image...");

        jButton3.setText("Remove Image");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Upload Image...");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(photoLbl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(275, Short.MAX_VALUE)
                .add(jButton3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton4)
                .add(145, 145, 145))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(photoLbl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 217, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 85, Short.MAX_VALUE)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton4)
                    .add(jButton3))
                .add(27, 27, 27))
        );

        infoTabbedPane.addTab("Image", jPanel2);

        jButton2.setText("Revert Changes");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton5.setText("Add");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Remove");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(73, 73, 73)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(39, 39, 39)
                        .add(jButton5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton6))
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 260, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(40, 40, 40)
                                .add(infoTabbedPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(244, 244, 244)
                                .add(jButton2)
                                .add(17, 17, 17)
                                .add(jButton1)))))
                .addContainerGap(133, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(31, 31, 31)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 440, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(30, 30, 30)
                        .add(infoTabbedPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 380, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 38, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jButton2)
                            .add(jButton1))))
                .add(9, 9, 9)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton6)
                    .add(jButton5))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ratingTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ratingTxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ratingTxtActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        handleSaveEvent();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        updateSelectedMenuItem();
        updateMenuItemFields();
    }//GEN-LAST:event_jTable1MousePressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        updateMenuItemFields();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        handleRemoveImage();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        handleAddMenuItem();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        handleRemoveMenuItem();
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        handleUploadImageClick();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void priceTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priceTxtActionPerformed

    }//GEN-LAST:event_priceTxtActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea descriptionTxt;
    private javax.swing.JPanel generalPanel;
    private javax.swing.JTabbedPane infoTabbedPane;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField nameTxt;
    private javax.swing.JLabel photoLbl;
    private javax.swing.JTextField priceTxt;
    private javax.swing.JTextField ratingTxt;
    private javax.swing.JTextField txtCookTime;
    // End of variables declaration//GEN-END:variables

    private TableModel getTableModel(){
        return tableModel;
    }

    private MenuItem getSelectedMenuItem() {
        int selectedIndex = jTable1.getSelectedRow();
        return tableModel.getItemAtIndex(selectedIndex);
    }

    private void updateSelectedMenuItem(){
        selectedMenuItem = getSelectedMenuItem();
    }

    private void updateSelectedMenuItemWithFormData(){
        if(selectedMenuItem == null) return;
        selectedMenuItem.setDescription(descriptionTxt.getText());
        selectedMenuItem.setName(nameTxt.getText());
        selectedMenuItem.setPhoto(photo);
        selectedMenuItem.setPrice(getPrice());
        selectedMenuItem.setTimeToCook(txtCookTime.getText());
    }

    private Double getPrice(){
        Double price = null;
        String priceStr = priceTxt.getText().replace("$", "").trim();
        try{
            price = Double.valueOf(priceStr);
        }catch(NumberFormatException ex){
            price = 0.0;
        }
        priceTxt.setText(Formatter.formatNumber(price));
        return price;
    }
    private void handleSaveEvent(){
        updateSelectedMenuItemWithFormData();
        saveSelectedMenuItem();
        refreshModel();
    }
    
    private void updateMenuItemFields() {
        if(selectedMenuItem == null){
            clearMenuItemFields();
        }else{
            String ratingStr = selectedMenuItem.getRating() == null ? "N/A" : selectedMenuItem.getRating().toString();
            nameTxt.setText(selectedMenuItem.getName());
            descriptionTxt.setText(selectedMenuItem.getDescription());
            priceTxt.setText(Formatter.formatNumber(selectedMenuItem.getPrice()));
            txtCookTime.setText(Formatter.format(selectedMenuItem.getTimeToCook()));
            ratingTxt.setText(ratingStr);
            photo = selectedMenuItem.getPhoto();
            updatePhotoLbl(photo);
        }
    }

    private void clearMenuItemFields() {
        nameTxt.setText("");
        descriptionTxt.setText("");
        priceTxt.setText(Formatter.formatNumber(null));
        txtCookTime.setText("");
        ratingTxt.setText("");
        photoLbl.setText("No Menu Item Selected.");
    }

    private void saveSelectedMenuItem() {
        if(selectedMenuItem == null) return;
        menuItemSvc.save(selectedMenuItem);
    }

    private void handleRemoveImage() {
        if(selectedMenuItem == null) return;
        photo = null;
        photoLbl.setIcon(null);
        photoLbl.setText("No Photo");
    }

    private void handleAddMenuItem() {
        selectedMenuItem = new MenuItem();
        selectedMenuItem.setRestaurant(RestaurantContext.getRestaurant());
        selectedMenuItem.setAvailable(true);
        List<MenuItem> menuItems = tableModel.getMenuItems();
        selectedMenuItem.setName("New Menu Item "+ (menuItems.size() + 1));
        tableModel.add(selectedMenuItem);
        updateMenuItemFields();
        handleSaveEvent();
        infoTabbedPane.setSelectedComponent(generalPanel);
    }

    private void handleRemoveMenuItem() {
        updateSelectedMenuItem();
        if(selectedMenuItem != null){
           menuItemSvc.remove(selectedMenuItem);
           refreshModel();
        }
    }

    public void refreshModel() {
        List<MenuItem> menuItems = menuItemSvc.getAvailableMenuItems(RestaurantContext.getRestaurant());
        tableModel.setMenuItems(menuItems);
        tableModel.fireTableDataChanged();
    }
    
    private void handleUploadImageClick() {
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg", "gif", "png");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            try {
                BufferedImage image = ImageIO.read(file);
                ImageDto dto = new ImageDto("temp", image);
                ImageDto savedDto = ServiceFactory.getPhotoService().save(dto);
                if(selectedMenuItem.getPhoto() == null){
                    selectedMenuItem.setPhoto(new Photo());
                }
                photo = new Photo();
                photo.setUrl(savedDto.getAbsolutePath());
                photo.setHeight(savedDto.getHeight());
                photo.setWidth(savedDto.getWidth());
                updatePhotoLbl(photo);
            } catch (IOException ex) {
                Logger.getLogger(MenuEditPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void updatePhotoLbl(Photo photo) {
        if(photo != null){
            ImageIcon icon;
            try {
                String url = photo.getUrl();
                BufferedImage bufferedImage;
                if(url.startsWith("/")){
                    bufferedImage = ImageIO.read(new File(photo.getUrl()));
                }else{
                    bufferedImage = ImageIO.read(new URL(photo.getUrl()));
                }
                icon = new ImageIcon(bufferedImage);
                photoLbl.setIcon(icon);
                photoLbl.setText("");
            } catch (IOException ex) {
                photoLbl.setIcon(null);
                photoLbl.setText("Error while loading Image");
            }

        }else{
            photoLbl.setIcon(null);
            photoLbl.setText("No Photo");
        }
    }


    private class MenuItemTableModel extends AbstractTableModel{
        private String[] columns= {"#","Name"};
        private List<MenuItem> menuItems;

        public MenuItemTableModel(){
            this(null);
        }

        public MenuItemTableModel(List<MenuItem> items) {
            menuItems = new ArrayList<MenuItem>();
            if(items != null){
                menuItems.addAll(items);
            }
        }

        @Override
        public int getRowCount() {
            return menuItems.size();
        }

        @Override
        public Object getValueAt(int row, int col) {
           MenuItem item = menuItems.get(row);
           Object value = null;
           if(item != null){
              switch(col){
                  case 0: 
                      value = row + 1;
                      break;
                  case 1:
                      value = item.getName();
                      break;
              }
           }
           return value;
        }

        @Override
        public int getColumnCount() {
          return columns.length;
        }

        @Override
        public String getColumnName(int col) {
          return columns[col];
        }

        private MenuItem getItemAtIndex(int selectedIndex) {
            if(selectedIndex == -1) return null;
            return menuItems.get(selectedIndex);
        }

        public void setMenuItems(List<MenuItem> menuItems){
            this.menuItems = menuItems;
            this.fireTableDataChanged();
        }

        public List<MenuItem> getMenuItems(){
            return menuItems;
        }

        public void add(MenuItem menuItem){
            this.menuItems.add(menuItem);
            this.fireTableDataChanged();
        }

    }
}
